"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const FSTool_1 = __importDefault(require("../lib/FSTool"));
const path_1 = __importDefault(require("path"));
const stringMd5_1 = __importDefault(require("../lib/FSTool/stringMd5"));
const configTool_1 = __importDefault(require("../com/configTool"));
const colors_console_1 = __importDefault(require("colors-console"));
const CryptoUtil_1 = __importDefault(require("../lib/CryptoUtil"));
class Authentication {
    static serverId = '';
    static async getServerId() {
        await configTool_1.default.init();
        if (Authentication.serverId) {
            return Authentication.serverId;
        }
        const SerialNumber = configTool_1.default.serverInfo.machineId;
        this.serverId = (0, stringMd5_1.default)(SerialNumber);
        return this.serverId;
    }
    static async Verification() {
        await configTool_1.default.init();
        const serverId = await Authentication.getServerId();
        const AESPassword = 'cbAK8wDRwiXkBxnDHUquiRjHyceR3b';
        const insideAuthorizationCode = (0, stringMd5_1.default)(serverId);
        return new Promise(async (resolve) => {
            try {
                const enLicenseObjTxt = FSTool_1.default.readFileSync(path_1.default.join(configTool_1.default.appBasePath, '/temp/serverId.lic')).toString();
                try {
                    const licenseObjTxt = CryptoUtil_1.default.Decrypt(enLicenseObjTxt, AESPassword);
                    const licenseObj = JSON.parse(licenseObjTxt);
                    const currentDate = new Date().toISOString().split('T')[0];
                    const beginDate = new Date(licenseObj.beginDate).toISOString().split('T')[0];
                    const endDate = new Date(licenseObj.endDate).toISOString().split('T')[0];
                    if (currentDate < beginDate || currentDate > endDate) {
                        console.log((0, colors_console_1.default)('red', `【授权码，错误！！】当前日期不在授权范围内，当前日期：` + new Date().toISOString()));
                        resolve(false);
                    }
                    else if (licenseObj.outAuthorizationCode !== insideAuthorizationCode) {
                        console.log((0, colors_console_1.default)('red', `【授权码，验证失败】设备标识码不匹配，当前日期：` + new Date().toISOString()));
                        resolve(false);
                    }
                    else {
                        const lastDay = Math.floor((new Date(endDate) - new Date(currentDate)) / (1000 * 60 * 60 * 24));
                        console.log((0, colors_console_1.default)('green', `【外部授权码，校验成功】：还剩 ${lastDay} 天使用期，授权文件位置：${path_1.default.join(configTool_1.default.appBasePath, '/temp/serverId.lic')}`));
                        resolve(licenseObj);
                    }
                }
                catch (e) {
                    console.log((0, colors_console_1.default)('red', `【授权码，错误！！】当前日期：` + new Date().toISOString()));
                    resolve(false);
                }
            }
            catch (e) {
                console.log((0, colors_console_1.default)('red', `权限错误：在路径【${path_1.default.join(configTool_1.default.appBasePath, '/temp/serverId.lic')}】下，找不到授权文件 serverId.lic，系统启动失败！！`));
                resolve(false);
            }
        });
    }
}
exports.default = Authentication;
